package day07;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author zouran
 * createDate：2024/6/28  21:08
 */
public class FileWrite {
    public static void example01() throws IOException {
        OutputStream out = new FileOutputStream("example.txt", true);
        StringBuilder strings = new StringBuilder();
        strings.append("你好,世界!").append("\n").append("This is an example!");
        out.write(strings.toString().getBytes(StandardCharsets.UTF_8));
        out.close();
    }

    public static void example02() throws IOException {
        OutputStream out = new FileOutputStream("example.txt", true);
        BufferedOutputStream outputStream = new BufferedOutputStream(out);
        StringBuilder strings = new StringBuilder();
        strings.append("你好,世界!").append("\n").append("This is an example!");
        outputStream.write(strings.toString().getBytes(StandardCharsets.UTF_8));
        outputStream.close();
    }

    public static void example03() throws IOException {
        FileWriter fileWriter = new FileWriter("example.txt", true);
        fileWriter.append("你好,世界!").append("\n").append("This is an example!");
        fileWriter.close();
    }

    public static void example04() throws IOException {
        FileWriter fileWriter = new FileWriter("example.txt", true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.append("你好,世界!").append("\n").append("This is an example!");
        bufferedWriter.close();
        fileWriter.close();
    }

    public static void main(String[] args) throws IOException {
        example01();
        example02();
        example03();
        example04();
    }
}
